package org.jeecg.modules.demo.cashier;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.IpUtils;
import org.jeecg.modules.demo.mall.entity.YxStoreCouponUser;
import org.jeecg.modules.demo.mall.entity.YxSystemUserLevel;
import org.jeecg.modules.demo.mall.entity.YxUser;
import org.jeecg.modules.demo.mall.yxstorecoupon.service.IYxStoreCouponService;
import org.jeecg.modules.demo.mall.yxsystemuserlevel.service.IYxSystemUserLevelService;
import org.jeecg.modules.demo.mall.yxuser.service.IYxUserService;
import org.jeecg.modules.demo.weixin.api.WeixinHelloApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 收银台 会员管理
 * @Author: jeecg-boot
 * @Date: 2023-10-24
 * @Version: V1.0
 */
@RestController
@RequestMapping("/mall/cashieruser/cashierUser")
@Slf4j
public class CashierUserController {
    @Autowired
    private IYxUserService yxUserService;

    @Autowired
    private WeixinHelloApi weixinHelloApi;
    @Autowired
    private IYxSystemUserLevelService yxSystemUserLevelService;

    @Autowired
    private IYxStoreCouponService yxStoreCouponService;


    /**
     * @param yxUser
     * @param request
     * @return
     */
    @ApiOperation(value = "会员添加", notes = "会员添加")
    @PostMapping(value = "/yxUserAdd")
    public Result<String> yxUserAdd(@RequestBody YxUser yxUser, HttpServletRequest request) {
        QueryWrapper<YxUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", yxUser.getPhone());
        queryWrapper.eq("is_del", 0);
        List<YxUser> list = yxUserService.list(queryWrapper);
        if (list.size() > 0) {
            return Result.error("该用户已在当前门店注册为会员！");
        }

        yxUser.setMerchantId(Integer.parseInt(yxUser.getMerId()));
        yxUser.setLastIp(IpUtils.getIpAddr(request));
        yxUserService.save(yxUser);
//        //保存会员等级记录表
//        YxUserLevel yxUserLevel = new YxUserLevel();
//        BeanUtils.copyProperties(yxUser, yxUserLevel);
//        yxUserLevel.setLevelId(yxUser.getLevel());
//        yxUserLevel.setMark(yxUser.getMark());
//        yxUserLevel.setMerId(yxUser.getMerchantId());
//        yxUserLevelService.save(yxUserLevel);
        return Result.OK("添加成功！");
    }

    /**
     * 根据商户id查询商户下的会员
     */
    @ApiOperation(value = "根据商户id查询商户下的会员", notes = "根据商户id查询商户下的会员")
    @GetMapping(value = "/queryUserByMerchantId")
    public Result<?> queryUserByMerchantId(YxUser yxUser,
                                           @RequestParam(name = "current", defaultValue = "1") Integer current,
                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                           HttpServletRequest req) {
        QueryWrapper<YxUser> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("merchant_id", Integer.parseInt(yxUser.getMerId()));
        if (StringUtils.isNotBlank(yxUser.getNickname())) {
            queryWrapper.like("nickname", yxUser.getNickname());
        }
        if (StringUtils.isNotBlank(yxUser.getPhone())) {
            queryWrapper.like("phone", yxUser.getPhone());
        }
        queryWrapper.eq("status", 1);
        if (StringUtils.isNotBlank(yxUser.getRealName())) {
            queryWrapper.like("real_name", yxUser.getRealName());
        }
        queryWrapper.orderByDesc("create_time");
        IPage<YxUser> page = new Page<>(current, pageSize);
        IPage<YxUser> pageList = yxUserService.page(page, queryWrapper);
        if (CollectionUtils.isEmpty(pageList.getRecords())) {
            return Result.OK(pageList);
        }
        List<YxUser> yxUserList = pageList.getRecords().stream().map(item -> {
//            Integer count = weixinHelloApi.queryCount(item.getUid());
//            if (count != null) {
//                //获取次卡的数量
//                item.setCartCount(count);
//            }
//            YxSystemUserLevel yxSystemUserLevel = yxSystemUserLevelService.getById(item.getLevel());
//            if (yxSystemUserLevel != null) {
//                item.setGrade(yxSystemUserLevel.getGrade());
//                item.setLevelName(yxSystemUserLevel.getLevelName());
//                item.setDiscount(yxSystemUserLevel.getDiscount());
            YxSystemUserLevel yxSystemUserLevel = yxSystemUserLevelService.queryLevel(item.getUid(), item.getUserGrowthValue());
            if (yxSystemUserLevel != null) {
                item.setGrade(yxSystemUserLevel.getGrade());
                item.setLevelName(yxSystemUserLevel.getName());
                item.setDiscount(yxSystemUserLevel.getDiscount());
                item.setLevel(yxSystemUserLevel.getId());
                yxUserService.updateById(item);
            }
            //获取用户卡点
            BigDecimal bigDecimal = weixinHelloApi.queryPoint(item.getUid(), yxUser.getMerId());
            item.setPoint(bigDecimal);

            return item;
        }).collect(Collectors.toList());
        return Result.OK(pageList.setRecords(yxUserList));
    }


    /**
     * 通过卡号查询
     *
     * @param phone
     * @return
     */
    @ApiOperation(value = "通过卡号查询", notes = "通过卡号查询")
    @GetMapping(value = "/queryByPhone")
    public Result< List<YxUser>> queryById(@RequestParam String phone) {
        LambdaQueryWrapper<YxUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(phone), YxUser::getPhone, phone);
       List<YxUser>   yxUserList = yxUserService.list(queryWrapper);
        return Result.OK(yxUserList);
    }


    /**
     * 通过id查询用户信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "yx_user-通过id查询用户信息", notes = "yx_user-通过id查询用户信息")
    @GetMapping(value = "/queryById")
    public Result<YxUser> queryById(@RequestParam(name = "id", required = true) Integer id, String merId) {
        YxUser yxUser = yxUserService.getById(id);
        if (yxUser == null) {
            return Result.error("未找到对应数据");
        }
        Integer count = weixinHelloApi.queryCount(yxUser.getUid(), merId);
        if (count != null) {
            //获取次卡的数量
            yxUser.setCartCount(count);
        }
        YxSystemUserLevel yxSystemUserLevel = yxSystemUserLevelService.getById(yxUser.getLevel());
        if (yxSystemUserLevel != null) {
            yxUser.setGrade(yxSystemUserLevel.getGrade());
            yxUser.setLevelName(yxSystemUserLevel.getName());
            yxUser.setDiscount(yxSystemUserLevel.getDiscount());
        }
//        List<YxStoreCouponUser> yxStoreCouponUsers = yxStoreCouponService.queryUserCoupon(merId, id);
//        if (!CollectionUtils.isEmpty(yxStoreCouponUsers)) {
//            yxUser.setCouponNum(yxStoreCouponUsers.size());
//        }

        //获取储值卡的数量
        BigDecimal bigDecimal = weixinHelloApi.queryPoint(id, merId);
        yxUser.setPoint(bigDecimal);

        return Result.OK(yxUser);
    }

    /**
     * 等级查询list
     *
     * @param
     * @return
     */
    @ApiOperation(value = "yx_user_level-等级查询", notes = "yx_user_level-等级查询")
    @GetMapping(value = "/queryLevelList")
    public Result<List<YxSystemUserLevel>> queryPageList() {
        QueryWrapper<YxSystemUserLevel> queryWrapper = new QueryWrapper<>();
        List<YxSystemUserLevel> levelsList = yxSystemUserLevelService.list(queryWrapper);
        return Result.OK(levelsList);
    }
}
